//
//  SISSReportViewController.m
//  SISSAPP
//
//  Created by 优房网 on 2017/5/15.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSReportViewController.h"
#import "SISSNetworkingManager+Home.h"
#import "SISSTextView.h"

@interface SISSReportViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet SISSTextView *textView;

/* 举报内容数组 */
@property (strong, nonatomic) NSArray *reportHouseArray;
@property (strong, nonatomic) NSArray *reportPassengerArray;
@property (strong, nonatomic) NSArray *reportRentOutArray;
@property (strong, nonatomic) NSArray *reportTenantArray;
/* selectIndexPath */
@property (assign, nonatomic) NSIndexPath *selectIndexPath;

@property (weak, nonatomic) IBOutlet UIButton *confirmButton;
@end

@implementation SISSReportViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"举报";
    
    self.reportHouseArray = @[@"已成交", @"售价不真实", @"房源不存在", @"房源信息与实际不符"];
    self.reportPassengerArray = @[@"已成交", @"预算不真实", @"客源不存在", @"客源信息与实际不符"];
    self.reportRentOutArray = @[@"已成交", @"租金不真实", @"出租不存在", @"出租信息与实际不符"];
    self.reportTenantArray = @[@"已成交", @"预算不真实", @"租客不存在", @"租客信息与实际不符"];
}

- (void)setupSubviews {
    [super setupSubviews];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    self.textView.placeholder = @"请简单描述详情，以便我们核查。";
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.reportHouseArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *reuseID = @"reportCellReuseID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
        cell.textLabel.textColor = COLOR(0x334A60);
        
        UIView *lineView = [[UIView alloc] init];
        lineView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [cell.contentView addSubview:lineView];
        
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(cell.contentView);
            make.height.mas_equalTo(1);
        }];
    }
    
    NSString *textStr = @"";
    if (self.houseModel) {
        textStr = self.reportHouseArray[indexPath.row];
    } else if (self.passengerModel) {
        textStr = self.reportPassengerArray[indexPath.row];
    } else if (self.rentOutModel) {
        textStr = self.reportRentOutArray[indexPath.row];
    } else if (self.tenantModel) {
        textStr = self.reportTenantArray[indexPath.row];
    }
    cell.textLabel.text = textStr;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.view endEditing:YES];
    // 之前选中的，取消选择
    UITableViewCell *celled = [tableView cellForRowAtIndexPath:self.selectIndexPath];
    celled.textLabel.textColor = COLOR(0x334A60);
    // 记录当前选中的位置索引
    self.selectIndexPath = indexPath;
    // 当前选择改变颜色
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (self.selectIndexPath == indexPath) {
        cell.textLabel.textColor = COLOR(0x3598AF);
    } else {
        cell.textLabel.textColor = COLOR(0x334A60);
    }
}

/**
 确定按钮点击事件
 */
- (IBAction)confirmButtonClickAction:(id)sender {
    
    if (self.textView.text.length == 0) {
        [SISSTools showSVErrorTitle:@"请填写补充说明" andTime:2 andColor:YES];
        return;
    }
    
    if ([SISSUserAccount shared].userName == nil || [SISSUserAccount shared].access_token == nil) {
        return;
    }
    
    NSDictionary *parameters;
    if (self.houseModel) {
        
        parameters = @{
                       @"saId":[SISSUserAccount shared].saId,
                       @"ahrId":self.houseModel.ID,
                       @"abdId":@"-1",
                       @"alrId":@"-1",
                       @"arrId":@"-1",
                       @"house_type":@(self.houseModel.houseresType),
                       @"demand_type":@(-1),
                       @"lease_type":@(-1),
                       @"rent_type":@(-1),
                       @"reason":self.reportHouseArray[self.selectIndexPath.row],
                       @"reportType":@(1),
                       @"otherdesc":self.textView.text
                       };
    } else if (self.passengerModel) {
        parameters = @{
                       @"saId":[SISSUserAccount shared].saId,
                       @"ahrId":@"-1",
                       @"abdId":self.passengerModel.ID,
                       @"alrId":@"-1",
                       @"arrId":@"-1",
                       @"house_type":@(-1),
                       @"demand_type":@(self.passengerModel.demandType),
                       @"lease_type":@(-1),
                       @"rent_type":@(-1),
                       @"reason":self.reportPassengerArray[self.selectIndexPath.row],
                       @"reportType":@(2),
                       @"otherdesc":self.textView.text
                       };
    } else if (self.rentOutModel) {
        parameters = @{
                       @"saId":[SISSUserAccount shared].saId,
                       @"ahrId":@"-1",
                       @"abdId":@"-1",
                       @"alrId":self.rentOutModel.ID,
                       @"arrId":@"-1",
                       @"house_type":@(-1),
                       @"demand_type":@(-1),
                       @"lease_type":@(self.rentOutModel.leaseType),
                       @"rent_type":@(-1),
                       @"reason":self.reportPassengerArray[self.selectIndexPath.row],
                       @"reportType":@(3),
                       @"otherdesc":self.textView.text
                       };
    } else if (self.tenantModel) {
        parameters = @{
                       @"saId":[SISSUserAccount shared].saId,
                       @"ahrId":@"-1",
                       @"abdId":@"-1",
                       @"alrId":@"-1",
                       @"arrId":self.tenantModel.ID,
                       @"house_type":@(-1),
                       @"demand_type":@(-1),
                       @"lease_type":@(-1),
                       @"rent_type":@(self.tenantModel.rentType),
                       @"reason":self.reportPassengerArray[self.selectIndexPath.row],
                       @"reportType":@(4),
                       @"otherdesc":self.textView.text
                       };
    }
    
    [kNetworkManager reportRecordWithparameters:parameters success:^(id responseObject) {
        
        if ([responseObject[@"code"] integerValue] == 200) {
            
            [SISSTools showSVSuccessTitle:@"举报成功！" andTime:2 andColor:YES];
            NSLog(@"举报成功！");
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                
                [self.navigationController popViewControllerAnimated:YES];
            });
        }
    } failed:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
}

@end
